#!/usr/bin/python
# -*- coding: UTF-8 -*-

from common.my_time import MyTime
from common.custom_task_data import CustomTaskData
import os
import shutil
import time
import datetime

class CustomTaskIndex():
    def __init__(self):
        self.custom_task = CustomTaskData()
        
    def __del__(self):
        pass

    def write_file_head(self, file_handler, task_date):
        keyword = "say by data,资讯"
        title = task_date + "资讯汇总"
        file_handler.write("<?php\n")
        file_handler.write("require_once rtrim($_SERVER['DOCUMENT_ROOT'],'/').'/site/config.php';\n")
        file_handler.write("include_once GLOBAL_PHP.'view.php';\n")
        file_handler.write("DisplayHead(array('keyword' => '" + keyword + "'));\n")
        file_handler.write("?>\n")
        file_handler.write("<div class=\"div_main_frame\" style=\"margin:0px;padding:12px;width:95%\">")
        file_handler.write("<div class=\"div_link_rank_title\" style=\"line-height:46px;font-size:30px;text-align:center;\">" + title + "</div>\n")

    def write_file_tail(self, file_handler):
        file_handler.write("<div style=\"clear:both\"></div></div>\n")
        file_handler.write("<?php\n")
        file_handler.write("DisplayTail();\n")
        file_handler.write("?>\n")

    def record_task_data(self, file_handler, task_date, entity_name, start_date, custom_word, url):
        file_handler.write("<div class=\"div_link_rank_content\" style=\"margin:0px;padding:0px;\"><a href=\"" + url + "\">[" + entity_name + "]["+ custom_word + "][" + str(start_date) + "~" + str(task_date) + "]</a></div>\n")

    def record_task_date(self, file_handler, task_date):
        file_handler.write("<div class=\"div_link_rank_title\" style=\"margin:0px;padding:0px;\">" +  str(task_date) + "</div>\n")

    def record_task_class(self, file_handler, class_id):
        ids  = {"100":"企业",
                "101":"机构",
                "200":"人物",
                "300":"国际",
                "400":"科学",
                "500":"文化",
                "600":"社会"}
        name = "未分类"
        if class_id in ids.keys():
            name = ids[class_id]
        file_handler.write("<div class=\"div_link_rank_title\" style=\"margin:0px;padding:0px;\">" +  name + "</div>\n")


    def process_date_index(self):
        t = datetime.datetime.now()
        for i in range(-5, 1):
            pre_date = (t + datetime.timedelta(days=i)).strftime("%Y-%m-%d")
            tasks = self.custom_task.query_email_task_by_date(str(pre_date))
            if len(tasks) == 0:
                continue;
            file_name = "index.php"
            file_handler = open(file_name, 'w', encoding = 'utf-8')
            self.write_file_head(file_handler, pre_date)
            for task in tasks:
                self.record_task_data(file_handler, task[1], task[2], task[3], task[4], task[5])
            self.write_file_tail(file_handler)
            file_handler.close()
            shutil.move(file_name, "/home/saybydata_static/" + str(pre_date) + "/" + file_name)

    def process_index(self):
        t = datetime.datetime.now()
        file_name = "index.php"
        file_handler = open(file_name, 'w', encoding = 'utf-8')
        self.write_file_head(file_handler, "近期")
        limit_date = (t + datetime.timedelta(days=-20)).strftime("%Y-%m-%d")
        class_set = self.custom_task.query_email_class(limit_date)
        for c in class_set:
            tasks = self.custom_task.query_email_task_by_class(c[0], limit_date)
            self.record_task_class(file_handler, str(c[0]))
            for task in tasks:
                self.record_task_data(file_handler, task[1], task[2], task[3], task[4], task[5])
        '''
        for i in range(0, -11, -1):
            pre_date = (t + datetime.timedelta(days=i)).strftime("%Y-%m-%d")
            tasks = self.custom_task.query_email_task_by_date(str(pre_date))
            if len(tasks) == 0:
                continue;
            self.record_task_date(file_handler, str(pre_date))
            for task in tasks:
                self.record_task_data(file_handler, task[1], task[2], task[3], task[4], task[5])
        '''
        self.write_file_tail(file_handler)
        file_handler.close()
        shutil.move(file_name, "/home/saybydata_static/" + file_name)


def custom_task_index_api():
    task = CustomTaskIndex()
    task.process_date_index()
    task.process_index()

if __name__ == '__main__':
    custom_task_index_api()
